package com.acme.bankapp.domain;
import java.io.Serializable;

import com.acme.bankapp.domain.Account;
import com.acme.bankapp.exception.*;
/**
 * Creating methods public boolean equals(Object obj) and public int hashCode() is requirement business logic and for further development of the project.
 * @author Troitskaya Anastasia
 *
 */
public class CheckingAccount extends Account {
	private double balance;
	private double overdraft;
	private final double overdraftFinal;
	CheckingAccount(double startBalance, double startOverdraft) {
		super();
		this.balance = startBalance;
		this.overdraft = startOverdraft;
		this.overdraftFinal = startOverdraft;
	}
	@Override
	void deposite(double delta) {
		this.balance += delta;
	}
	@Override
	void withdraw(double delta) throws OverDraftLimitExceededException {
		if (this.balance+this.overdraft-delta < -this.overdraftFinal) {
			throw new OverDraftLimitExceededException(this.balance, this.overdraftFinal+this.overdraft+this.balance);
		}
		else {
			if (this.overdraft >= 0) {
				if (this.balance >= delta)
					this.balance -= delta;
				else 
					if (this.overdraft >= (delta - this.balance)) {
						this.overdraft -= delta - this.balance;
						this.balance = 0;
					}
					else {
						this.balance -= delta - this.overdraft;
						this.overdraft = 0;
					}
			}
		}
	}
	@Override
	public double getBalance() {
		return this.balance;
	}
	@Override
	public double maxCredit() {
		return this.overdraftFinal+this.balance+this.overdraft;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(overdraft);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(overdraftFinal);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CheckingAccount other = (CheckingAccount) obj;
		if (Double.doubleToLongBits(balance) != Double
				.doubleToLongBits(other.balance))
			return false;
		if (Double.doubleToLongBits(overdraft) != Double
				.doubleToLongBits(other.overdraft))
			return false;
		if (Double.doubleToLongBits(overdraftFinal) != Double
				.doubleToLongBits(other.overdraftFinal))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "CheckingAccount [balance=" + balance + ", overdraft="
				+ overdraft + ", overdraftFinal=" + overdraftFinal + ", maxCredit = "+(overdraftFinal+balance+overdraft)+"]";
	}
}
